package com.sw.test01.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.test01.R;
import com.sw.test01.domain.bean.Msg;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xifanoo on 2017/3/22.
 */

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> msgList = new ArrayList<>();

    public MsgAdapter(List<Msg> msgList) {
        this.msgList = msgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_msg, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = msgList.get(position);
        switch (msg.getType()){
            case Msg.TYPE_SENT:
                holder.rlReceived.setVisibility(View.GONE);
                holder.tvSend.setText(msg.getContent());
                break;
            case Msg.TYPE_RECEIVED:
                holder.rlSend.setVisibility(View.GONE);
                holder.tvReceived.setText(msg.getContent());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return msgList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rlSend;
        RelativeLayout rlReceived;
        TextView tvSend;
        TextView tvReceived;

        public ViewHolder(View view) {
            super(view);
            rlSend = (RelativeLayout) view.findViewById(R.id.rl_send);
            rlReceived = (RelativeLayout) view.findViewById(R.id.rl_received);
            tvSend = (TextView) view.findViewById(R.id.tv_send);
            tvReceived = (TextView) view.findViewById(R.id.tv_received);
        }
    }
}
